package common.activiti.web.action;

import java.util.List;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import common.hibernate.vo.Page;
import common.web.action.CommonAction;
import common.web.vo.Result;

/**
 * 流程实例管理控制器
 * User: henryyan
 */
@Controller
@RequestMapping(value = "/bpm/processinstance")
public class ProcessInstanceManagerController extends CommonAction {

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    RepositoryService repositoryService;

    /**
     * 流程实例列表
     */
    @RequestMapping(value = "list")
    @ResponseBody
    public Result processInstanceList(@RequestBody JSONObject json) {

        Page<ProcessInstance> page = new Page<ProcessInstance>();
        page.setPageSize(super.getPageSize(json));
		page.setCurrentPage(super.getPageNumber(json));
		
        ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();
        List<ProcessInstance> processInstanceList = processInstanceQuery.listPage(page.getStart(), page.getPageSize());
        Result res = Result.createPageResult(page);

        page.setRows(processInstanceList);
        page.setTotal(Long.valueOf(processInstanceQuery.count()).intValue());
        return res;
    }

    /**
     * 更改流程实例的状态
     *
     * @return
     */
    @RequestMapping(value = "{state}/{processInstanceId}")
    @ResponseBody
    public Result changeState(@PathVariable("state") String state,
                              @PathVariable("processInstanceId") String processInstanceId) {
        if (StringUtils.equals("active", state)) {
            runtimeService.activateProcessInstanceById(processInstanceId);
        } else {
            runtimeService.suspendProcessInstanceById(processInstanceId);
        }

        return Result.SUCCESS;
    }

    /**
     * 删除流程实例
     *
     * @return
     */
    @RequestMapping(value = "delete/{processInstanceId}")
    @ResponseBody
    public Result delete(@PathVariable("processInstanceId") String processInstanceId, @RequestBody JSONObject json) {
    	String deleteReason = json.getString("deleteReason");
        runtimeService.deleteProcessInstance(processInstanceId, deleteReason);
        return Result.SUCCESS;
    }

    /**
     * 流程定义对象缓存
     *
     * @param definitionMap
     * @param processDefinitionId
     */
    @RequestMapping(value = "view/{processDefinitionId}")
    @ResponseBody
    public Result definitionCache(@PathVariable("processDefinitionId") String processDefinitionId) {
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        processDefinitionQuery.processDefinitionId(processDefinitionId);
        ProcessDefinition processDefinition = processDefinitionQuery.singleResult();
        return Result.createResult(processDefinition);
    }

}
